#ifndef     __THREAD_H
#define     __THREAD_H

#include "../rtx/rtx_os.h"
#include "../utils/nocopyable.h"

namespace rtos {

template <typename Task, uint32_t StackSize, osPriority_t Priority, uint32_t Attr = osThreadDetached>
class Thread_t : private utils::NoCopyable_t {

public:
    
    inline void start(const char *name = "other") {
        osThreadAttr_t attr;
        attr.name = name;
        attr.attr_bits = Attr;
        attr.cb_mem = m_cb_mem;
        attr.cb_size = osRtxThreadCbSize;
        attr.stack_mem = m_stack_mem;
        attr.stack_size = StackSize;
        attr.priority = Priority;
        attr.tz_module = 0;
        attr.reserved = 0;
        m_id = osThreadNew(exec_main, this, &attr);
    }

    static void exec_main(void *arg) {
        static_cast<Task *>(arg)->run();
    }

private:
    osThreadId_t m_id;
    uint64_t m_cb_mem[(osRtxThreadCbSize + 7) / 8];
    uint64_t m_stack_mem[(StackSize + 7) / 8];

};

}

#endif
